/*
Project: 		The Impact of Short-Term Employment for Low-Income Youth: Experimental 	
				Evidence from the Philippines
Authors: 		Emily A. Beam and Stella Quimbo

***********************************
Path: 			code/02_analysis
File name: 		table_a08.do 
***********************************

Purpose: 		Generates Appendix Table A08 

Inputs: 		spes_data.dta
Outputs: 		table_a08.tex

Log: 			results/03_output/log_table_a08.log		

Last updated: 	18 November 2021

*/ 

cap log close 

log using "$output/log_table_a08.log", replace 

use "$usedata_analysis/spes_data",clear 
	keep if endline == 1
	keep if _eeo_worknow == 1
	
	// Condtional on workin 


*** Set up local variables 

local l_bb_female "Female"
local l_NT_edu "College"

local intlist "_bb_female _NT_edu"

local n: word count $cov1
	assert `n' == 18 

** Interaction terms 

foreach interact in  `intlist' {
	
	foreach mes in tr spes_2016{
gen `mes'X`interact' = `mes'*`interact'
label var `mes'X`interact' "SPES X `l`interact''"

	}

assert !missing(`interact')

gen `interact'xtreatment = `interact'*treatment
label var `interact'xtreatment "SPES X `l`interact''"


* Generate inteaction terms for each covariate 

ds $cov1 
foreach cov in `r(varlist)'{ 
gen `interact'X`cov' = `cov'*`interact'
} 

}




* Set locals 
local depvar1 			_eeo_wage_mn 		_eeo_workhours  			
local labels1 "		"Currently earning"  			"Work hours weekly now" "
					


										
loc w = 1				
local name1 "employment" 


forval j = 1/1{		
					

local r replace



** Uninteracted version ** 
	foreach var in `depvar`j'' {
	
	
foreach mes in  spes_2016{

		xi:  ivregress 2sls  `var' (`mes' = treatment ) $cov1  i.scel if treatment!=. ,  robust
		estimates store iv`mes'`var'
		summ `var' if treatment==0 
		estadd scalar dmean = `r(mean)'


}
	
	}
	foreach var in `depvar`j'' {


foreach interact in `intlist'  {


		
foreach mes in  spes_2016{

	
	qui	xi:  ivregress 2sls  `var' (`mes' `mes'X`interact' = treatment `interact'xtreatment) $cov1 `interact'X* i.scel if treatment!=. & _f`interact' == 0,  robust
		
		estimates store iv`mes'`w'`interact'
		summ `var' if treatment==0 
		estadd scalar dmean = `r(mean)'
		
		test `mes' + `mes'X`interact' == 0 
		local pval = `r(p)'
		estadd scalar pvalint = `r(p)'
}	// nature of IV loop
		

	
	}		// interaction term
	
	local w = `w' + 1
}		// individual dependent variables within each set
	
}		// dependent variable sets (tables)	

		


********************************************
********************************************


** Table numbers 

loc tnemp = 3	//  employment 

*** Footnote text 


local fn`tnemp' = "\multicolumn{3}{p{0.7\textwidth}}{\footnotesize{Notes: Sample includes endline respondents who report positive earnings and work hours, respectively. All specifications include controls listed in Table \ref{tbal} along with stratification-cell fixed effects. Panels B and C add controls multiplied by the binary interaction term along with uninteracted stratification-cell fixed effects.  \textit{*** p$<$0.01, ** p$<$0.05, * p$<$0.10} }}\\"


********************************************
********************************************
cd "$tables_analysis"
#delimit ; 
********** Table 3 - employment  *************;

	
	
	// Output Tables ;

	
foreach mes in  spes_2016{;
	
	esttab 		iv`mes'_eeo_wage_mn 		iv`mes'_eeo_workhours  	
	using table`tnemp'_iv_`mes'_cond.tex, replace star(* 0.10 ** 0.05 *** 0.01) 
	cells("b(fmt(2) star)" "se(par([ ]))") 

	keep(`mes') label  noobs
	varwidth(16) modelwidth(13) style(tex) 
	title(Impact of SPES on employment\label{temployment}) 
	varlabels(_cons Constant) 
	mlabels("Current monthly earnings"  	"Work hours weekly now" , 
			span prefix(\multicolumn{@span}{x{0.15\textwidth}}{) suffix(})) 
	collabels(none)
	prehead( "\begin{tabular}{lcc}" "\toprule") 
	posthead(\hline) 
	prefoot() 
	postfoot("\bottomrule");
};


	// Panel B Female;
	

foreach mes in  spes_2016{;

	esttab iv`mes'1_bb_female 		iv`mes'2_bb_female		
	using table`tnemp'_iv_`mes'_bb_female_cond.tex, replace star(* 0.10 ** 0.05 *** 0.01) 
	cells("b(fmt(2) star)" "se(par([ ]))") 
	stats( pvalint , fmt(%9.3f  )  
	labels(  "p-value, SPES + SPES X Female")) 
	keep(`mes' `mes'X_bb_female) label  
	varwidth(16) modelwidth(13) style(tex) 
	title(Impact of SPES on employment\label{temployment}) 
	varlabels(_cons Constant) 
	mlabels(	
			"Current monthly earnings" 	"Work hours weekly now" , 
			span prefix(\multicolumn{@span}{x{0.15\textwidth}}{) suffix(})) 
	collabels(none)
	prehead("\begin{table}\caption{@title}" "\begin{center}"  "\begin{tabular}{lcc}" "\toprule") 
	posthead(\hline) 
	prefoot() 
	postfoot("\bottomrule"  );
};
	//* Panel C Education ;
	
foreach mes in  spes_2016{;

	esttab iv`mes'1_NT_edu		iv`mes'2_NT_edu 		
	using table`tnemp'_iv_`mes'_NT_edu_cond.tex, replace star(* 0.10 ** 0.05 *** 0.01) 
	cells("b(fmt(2) star)" "se(par([ ]))") 
	stats( pvalint N dmean , fmt( %9.3f %9.0f %9.2f )  
	labels("p-value, SPES + SPES X College" "Observations" "Mean, control group" )) 
	keep(`mes' `mes'X_NT_edu) label  varwidth(16) modelwidth(13) style(tex) 
	title(Impact of SPES on employment\label{temployment}) 
	varlabels(_cons Constant) 
	mlabels("Current monthly earnings"  			"Work hours weekly now" ,
			span prefix(\multicolumn{@span}{x{0.15\textwidth}}{) suffix(})) 
	collabels(none)
	prehead("\begin{table}\caption{@title}" "\begin{center}"  "\begin{tabular}{lcc}" "\toprule") 
	posthead(\hline) prefoot() postfoot("\bottomrule" "`fn`tnemp''" "\end{tabular}" );
};






// Combine panels ;


include "$dofiles/PanelCombine.do";



// Table 3  - IV - self report ;

panelcombine, use(table`tnemp'_iv_spes_2016_cond.tex table`tnemp'_iv_spes_2016_bb_female_cond.tex table`tnemp'_iv_spes_2016_NT_edu_cond.tex)  
columncount(3)  paneltitles("Aggregate treatment effects" "Interacted by gender" "Interacted by education level") cleanup
save(table_a08.tex) ;


log close;

exit;
